﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.XPath;
using System.Web;

namespace SBS.Base
{
    public partial class GlobalCache    
    {
        public static void CacheRemovedCallBack(string key, object value, System.Web.Caching.CacheItemRemovedReason reason)
        {
            SBS.Common.Log.WriteTxtLog("CacheRemovedLog", "monthly", key + "\r\n" + reason.ToString());
        }
        public static XmlDocument GetUserTree()
        {
            string usertree;
            if (HttpContext.Current == null)
            {
                usertree = Path.GetFullPath(AppDomain.CurrentDomain.BaseDirectory + @"..\xml.tasktoday.com\user\usertree.xml");
            }
            else
            {
                usertree = Path.GetFullPath(HttpContext.Current.Server.MapPath("/") + "../xml.tasktoday.com/user/usertree.xml");
            }
            if (!File.Exists(usertree)) return null;
            if (HttpRuntime.Cache[usertree] != null)
            {
                return (XmlDocument)HttpRuntime.Cache[usertree];
            }
            XmlDocument dom = new XmlDocument();
            dom.Load(usertree);
            System.Web.Caching.CacheDependency dep = new System.Web.Caching.CacheDependency(usertree);
            HttpRuntime.Cache.Insert(usertree, dom, dep, System.DateTime.Now.AddHours(24), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.High, new System.Web.Caching.CacheItemRemovedCallback(CacheRemovedCallBack));
            return (XmlDocument)HttpRuntime.Cache[usertree];
        }
        public static XmlDocument GetLeaveUsers()
        {
            string leaveusers;
            if (HttpContext.Current == null)
            {
                leaveusers = Path.GetFullPath(AppDomain.CurrentDomain.BaseDirectory + @"..\xml.tasktoday.com\user\usertreeleave.xml");
            }
            else
            {
                leaveusers = Path.GetFullPath(HttpContext.Current.Server.MapPath("/") + "../xml.tasktoday.com/user/usertreeleave.xml");
            }
            if (!File.Exists(leaveusers)) return null;
            if (HttpRuntime.Cache[leaveusers] != null)
            {
                return (XmlDocument)HttpRuntime.Cache[leaveusers];
            }
            XmlDocument dom = new XmlDocument();
            dom.Load(leaveusers);
            System.Web.Caching.CacheDependency dep = new System.Web.Caching.CacheDependency(leaveusers);
            HttpRuntime.Cache.Insert(leaveusers, dom, dep, System.DateTime.Now.AddHours(2), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Low, new System.Web.Caching.CacheItemRemovedCallback(CacheRemovedCallBack));
            return (XmlDocument)HttpRuntime.Cache[leaveusers];

        }
        public static XmlDocument GetSiteConfig()
        {
            string siteconfig = String.Empty;
            if (HttpContext.Current != null)
            {
                siteconfig = Path.GetFullPath(HttpContext.Current.Server.MapPath("/") + "../site.config");
            }
            else
            {
                string appfolder = AppDomain.CurrentDomain.BaseDirectory;
                if (File.Exists(appfolder + "site.config"))
                {
                    siteconfig = appfolder + "site.config";
                }
                else
                {
                    DirectoryInfo parent = new DirectoryInfo(appfolder);                    
                    if (File.Exists(parent.Parent.FullName + "\\site.config"))
                    {
                        siteconfig = parent.Parent.FullName + "\\site.config";
                    }
                    else
                    {
                        throw new NullReferenceException("Can not found site.config at the same folder or parent!");
                        return null;
                    }
                }
                
            }
            if (HttpRuntime.Cache[siteconfig] != null)
            {
                return (XmlDocument)HttpRuntime.Cache[siteconfig];
            }
            XmlDocument dom = new XmlDocument();
            dom.Load(siteconfig);            
            System.Web.Caching.CacheDependency dep = new System.Web.Caching.CacheDependency(siteconfig);
            HttpRuntime.Cache.Insert(siteconfig, dom, dep, System.DateTime.Now.AddHours(24), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.High, new System.Web.Caching.CacheItemRemovedCallback(CacheRemovedCallBack));
            return (XmlDocument)HttpRuntime.Cache[siteconfig];
        }
    }
}
